//
//  ZCTipsView.m
//  ZCLive
//
//  Created by 彭军 on 2017/11/12.
//  Copyright © 2017年 Elvis. All rights reserved.
//


#import "ZCPreTipsView.h"
#define isIOS7 [[[UIDevice currentDevice]systemVersion] floatValue] >= 7.0
#define MAXWIDTH 200     //提示信息最大显示宽度
#define TIPSFONT 17      //提示信息字体大小
#define DISMISSTIME 2    //提示信息显示时间

@interface ZCPreTipsView ()

@property(nonatomic, strong) UIView *bgView;
@property(nonatomic, strong) UILabel *tipsLabel;

@end

static ZCPreTipsView *singletonInstance = nil;
@implementation ZCPreTipsView

+ (ZCPreTipsView *)sharedInstance {
    static dispatch_once_t oncetoken;
    dispatch_once(&oncetoken,^{
        if (!singletonInstance) {
            singletonInstance = [[self alloc] init];
        }
    });
    return singletonInstance;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)init {
    self = [super init];
    if (self) {
        self.frame = [[UIApplication sharedApplication] keyWindow].bounds;
        //self.backgroundColor = [UIColor magentaColor];
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [[UIColor darkGrayColor] colorWithAlphaComponent:0.5];
        _bgView.layer.cornerRadius = 10.0;
        _bgView.layer.borderColor = [UIColor darkGrayColor].CGColor;
        _bgView.layer.borderWidth = 0.5;
        _bgView.layer.masksToBounds = YES;
        _tipsLabel = [[UILabel alloc] init];
        _tipsLabel.backgroundColor = [UIColor clearColor];
        _tipsLabel.textAlignment = NSTextAlignmentCenter;
        _tipsLabel.font = [UIFont systemFontOfSize:TIPSFONT];
        _tipsLabel.textColor = [UIColor whiteColor];
        _tipsLabel.numberOfLines = 0;
        [_bgView addSubview:_tipsLabel];
        [self addSubview:_bgView];
    }
    return self;
}

- (void)dealloc {
    [ZCPreTipsView cancelPreviousPerformRequestsWithTarget:self];
}

+ (void)showTips:(NSString *)tips {
    if ([tips length] == 0) {
        return;
    }
    [self sharedInstance];
    CGSize textSize = CGSizeZero;
    if (isIOS7) {
        NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:TIPSFONT]};
        textSize = [tips boundingRectWithSize:CGSizeMake(MAXWIDTH, MAXFLOAT)
                                             options:NSStringDrawingUsesLineFragmentOrigin
                                          attributes:attributes
                                             context:nil].size;
    }else{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        textSize = [tips sizeWithFont:singletonInstance.tipsLabel.font constrainedToSize:CGSizeMake(MAXWIDTH, MAXFLOAT) lineBreakMode:singletonInstance.tipsLabel.lineBreakMode];
#pragma clang diagnostic pop
    }
    CGRect tipsFrame = singletonInstance.tipsLabel.frame;
    tipsFrame.size = textSize;
    singletonInstance.tipsLabel.frame = tipsFrame;
    singletonInstance.tipsLabel.text = tips;
    textSize.width += 20.0;
    textSize.height += 20.0;
    CGRect bgFrame = singletonInstance.bgView.frame;
    bgFrame.size = textSize;
    singletonInstance.bgView.frame = bgFrame;
    singletonInstance.tipsLabel.center = [singletonInstance convertPoint:singletonInstance.bgView.center toView:singletonInstance.bgView];
    singletonInstance.bgView.center = singletonInstance.center;
    [[[UIApplication sharedApplication].windows objectAtIndex:0] addSubview:singletonInstance];
    [singletonInstance performSelector:@selector(dismiss) withObject:nil afterDelay:1];
}

- (void)dismiss {
    [UIView animateWithDuration:1.1 animations:^{
        singletonInstance.alpha = 0.0;
    } completion:^(BOOL finished) {
        [singletonInstance removeFromSuperview];
        singletonInstance.alpha = 1.0;
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
